﻿/**********************************************************
 * 模    块	：	常用扩展类。
 * 作    者	：	Jasper
 * 编写时间	：	2010-04-24
 * 实现功能	：	常用类进行方法扩展
 * 详细描述	：	对String类进行扩展，转化为HTML格式等。
 * 
 * *********************************************************/

using System;
using System.Text.RegularExpressions;


namespace Basic4Net
{
	public static class Extensions
	{
		#region String扩展
		/// <summary>
		/// 获取一个字符串中出现某字符的次数
		/// </summary>
		/// <param name="source"></param>
		/// <param name="c"></param>
		/// <returns></returns>
		public static int _IndexCount(this String source, char c)
		{
			int count = 0;
			//foreach (char ch in source.ToCharArray())
			//{
			//    if (ch == c)
			//        count++;
			//}
			System.Threading.Tasks.Parallel.ForEach(source.ToCharArray(), (ch) =>
			{
				if (ch == c)
					count++;
			});
			return count;
		}

		/// <summary>
		/// 将文本格式字符串转换为HTML格式字符串
		/// </summary>
		/// <param name="source">需要转换的文本字符串</param>
		/// <returns>转换后的HTML字符串</returns>
		public static string _HtmlEncode(this String source)
		{
			string text = source;

			text = text.Replace("&", "&amp;");
			text = text.Replace("<", "&lt;");
			text = text.Replace(">", "&gt;");
			text = text.Replace(" ", "&nbsp;");
			text = text.Replace("'", "&#39;");
			text = text.Replace("\"", "&quot;");
			text = text.Replace("\r\n", "<br />");
			text = text.Replace("\n", "<br/>");
			text = text.Replace("·", "&#183;");

			return text;
		}

		/// <summary>
		/// 将HTML格式字符串转换为文本格式字符串
		/// </summary>
		/// <param name="source">需要转换的HTML字符串</param>
		/// <returns>转换后的文本字符串</returns>
		public static string _HtmlDecode(this String source)
		{
			string text = source;

			text = text.Replace("<br />", "\r\n");
			text = text.Replace("&quot;", "\"");
			text = text.Replace("&#39;", "'");
			text = text.Replace("&nbsp;", " ");
			text = text.Replace("&gt;", ">");
			text = text.Replace("&lt;", "<");
			text = text.Replace("&amp;", "&");
			text = text.Replace("&#183;", "·");

			return text;
		}

		/// <summary>
		/// 截断字符串
		/// </summary>
		/// <param name="source">需要截断的文本</param>
		/// <param name="substrLength">截断长度</param>
		/// <returns>截断后的文本</returns>
		public static string _Substr(this String source, int substrLength)
		{
			if (string.IsNullOrEmpty(source)) return String.Empty;
			if (source.Length <= substrLength) return source;
			return source.Substring(0, substrLength) + "...";
		}

		/// <summary>
		/// 判断字符串是否数字
		/// </summary>
		/// <param name="source">要判断的字符串</param>
		/// <returns>true为数字，false不为数字</returns>
		public static bool _IsNumber(this String source)
		{
			Regex r=new Regex(@"^-?\d+(\.)?\d*$");
			if (r.IsMatch(source))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 判断字符串是否日期
		/// </summary>
		/// <param name="source">要判断的字符串</param>
		/// <returns>true为数字，false不为数字</returns>
		public static bool _IsDate(this String source)
		{
			try
			{
				DateTime.Parse(source);
				return true;
			}
			catch 
			{

				return false;
			}
		}


		/// <summary>
		/// 返回Int32
		/// </summary>
		/// <param name="source">要转换的字符串</param>
		/// <returns></returns>
		public static int _ToInt(this String source)
		{
			try
			{
				return int.Parse(source);
			}
			catch
			{
				return -1;
			}
		}

		/// <summary>
		/// 返回Decimal
		/// </summary>
		/// <param name="source">要转换的字符串</param>
		/// <returns></returns>
		public static decimal _ToDecimal(this String source)
		{
			try
			{
				return decimal.Parse(source);
			}
			catch
			{
				return -1;
			}
		}

		/// <summary>
		/// 获取后缀名
		/// </summary>
		/// <param name="source">药获取后缀名的字符串</param>
		/// <returns></returns>
		public static string _GetExtension(this String source)
		{
			try
			{
				if (string.IsNullOrEmpty(source)) return String.Empty;
				int index = source.LastIndexOf('.');
				if (index == -1) return String.Empty;
				return source.Substring(index+1);
			}
			catch (Exception)
			{
				return String.Empty;
			}
		}

		/// <summary>
		/// js的escape方法
		/// </summary>
		/// <param name="source"></param>
		/// <returns></returns>
		public static string _escape(this String source)
		{
			try
			{
				if (string.IsNullOrEmpty(source)) return String.Empty;
				return Microsoft.JScript.GlobalObject.escape(source);
			}
			catch (Exception)
			{
				return String.Empty;
			}
		}

		/// <summary>
		/// js的unescape方法
		/// </summary>
		/// <param name="source"></param>
		/// <returns></returns>
		public static string _unescape(this String source)
		{
			try
			{
				if (string.IsNullOrEmpty(source)) return String.Empty;
				return Microsoft.JScript.GlobalObject.unescape(source);
			}
			catch (Exception)
			{
				return String.Empty;
			}
		}

		#endregion

		#region DateTime扩展

		public static string _ToWeekDayName(this DateTime source)
		{
			DayOfWeek dow = source.DayOfWeek;
			switch (dow)
			{
				case DayOfWeek.Monday:
					return "星期一";
				case DayOfWeek.Tuesday:
					return "星期二";
				case DayOfWeek.Wednesday:
					return "星期三";
				case DayOfWeek.Thursday:
					return "星期四";
				case DayOfWeek.Friday:
					return "星期五";
				case DayOfWeek.Saturday:
					return "星期六";
				case DayOfWeek.Sunday:
					return "星期日";
				default:
					return "";
			}
		}

		#endregion

		#region Socket扩展
		/// <summary>
		/// 发送变长数据
		/// </summary>
		/// <param name="source">套接字对象</param>
		/// <param name="Message">消息内容</param>
		/// <returns>发送字节数</returns>
		public static int _SendData(this System.Net.Sockets.Socket source, byte[] Message)
		{
			int total = 0;
			int size = Message.Length;
			int dataleft = size;
			int sent;

			while (total < size)
			{
				sent = source.Send(Message, total, dataleft, System.Net.Sockets.SocketFlags.None);
				total += sent;
				dataleft -= sent;
			}

			return total;
		}

		/// <summary>
		/// 接收变长数据
		/// </summary>
		/// <param name="Client">套接字对象</param>
		/// <returns>消息内容</returns>
		public static byte[] _ReceiveData(this System.Net.Sockets.Socket source)
		{
			int total = 0;
			int recv;
			byte[] datasize = new byte[17];
			recv = source.Receive(datasize, 0, datasize.Length, System.Net.Sockets.SocketFlags.None);

			if (recv == 0)
			{
				return null;
			}

			string len = System.Text.Encoding.Default.GetString(datasize, 1, 4);
			int size = Convert.ToInt32(len) - 17;

			int dataleft = size;
			byte[] data = new byte[size];
			while (total < size)
			{
				recv = source.Receive(data, total, dataleft, System.Net.Sockets.SocketFlags.None);
				if (recv == 0)
				{
					data = null;
					break;
				}
				total += recv;
				dataleft -= recv;
			}
			byte[] Result = new byte[data.Length + 17];
			for (int i = 0; i < 17; i++)
			{
				Result[i] = datasize[i];
			}
			for (int i = 0; i < data.Length; i++)
			{
				Result[17 + i] = data[i];
			}
			return Result;
		}
		#endregion
	}
}
